package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap;

import com.alibaba.cirtus.ots.application.adapter.ability.model.enums.MnSAPStatus;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.InfoUtil;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.RequestUtils;
import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.Nullable;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationadaptersdk.model.dto.CustomerStatementCallbackRequest;
import com.epoch.app.otsapplicationadaptersdk.model.dto.CustomerStatementQueryRequest;
import com.epoch.app.otsapplicationadaptersdk.model.dto.CustomerStatementQuerySDO;
import com.epoch.app.otsapplicationadaptersdk.sapv2.mncustomerstatement.dto.QueryRequest;
import com.epoch.app.otsapplicationadaptersdk.sapv2.mncustomerstatement.service.MnCustomerStatementService;
import com.epoch.app.otsapplicationadaptersdk.service.ApiAdapterService;
import com.epoch.app.otsapplicationreportsdk.customerstatementwrite.service.CustomerStatementWriteService;
import com.epoch.app.otsapplicationreportsdk.model.dto.CustomerStatementReportSDO;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @description: 销售公司客户对账单查询
 * @date 2023/10/8 15:30
 */
@Component
@Primary
public class MnCustomerStatementServiceImpl implements MnCustomerStatementService {
    private final Log log = Log.getLogger(MnCustomerStatementServiceImpl.class);

    @Resource
    private ApiAdapterService apiAdapterService;

    @Resource
    private CustomerStatementWriteService customerStatementWriteService;

    @Override
    public Object query(QueryRequest queryRequest) {
        CustomerStatementQueryRequest request = new CustomerStatementQueryRequest();
        request.setMessageHeader(RequestUtils.generateMessageHeader("SI_TPM150_CUSTOMERLIST_SUB_ASYN_OUT","ORZT","ECC"));
        CustomerStatementQuerySDO customerStatementQuerySDO = new CustomerStatementQuerySDO();
        customerStatementQuerySDO.setVKORG(queryRequest.getSalesOrganizationCode());
        customerStatementQuerySDO.setVTWEG(queryRequest.getChannelCode());
        customerStatementQuerySDO.setPRCTR(queryRequest.getProfitCenter());
        customerStatementQuerySDO.setKUNRG(queryRequest.getCustomerCode());
        customerStatementQuerySDO.setGJAHR(queryRequest.getYear());
        customerStatementQuerySDO.setMONAT(queryRequest.getMonth());
        request.setData(Lists.newArrayList(customerStatementQuerySDO));
        return apiAdapterService.reqSapCustomerStatementQuery(request);
    }


    @Override
    @ProcessInvoker(remark = "MnCustomerStatementService", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public Result<Void> doHandler(CustomerStatementCallbackRequest customerStatementCallbackRequest){
        if (Objects.isNull(customerStatementCallbackRequest.getRESULT())) {
            log.error("MnCustomerStatementServiceImpl_doHandler,结果RESULT为空, req={}", JSONObject.toJSONString(customerStatementCallbackRequest));
            return Result.fail("","结果RESULT为空");
        }

        if (!MnSAPStatus.SUCCESS.getCode().equals(customerStatementCallbackRequest.getRESULT().getFLAG())) {
            log.error("MnCustomerStatementServiceImpl_doHandler,查询FlAG返回失败, req={}", JSONObject.toJSONString(customerStatementCallbackRequest));
            return Result.fail(customerStatementCallbackRequest.getRESULT().getFLAG(),customerStatementCallbackRequest.getRESULT().getMESSAGE());
        }

        com.epoch.app.otsapplicationreportsdk.model.dto.CustomerStatementCallbackRequest callbackRequest = new com.epoch.app.otsapplicationreportsdk.model.dto.CustomerStatementCallbackRequest();
        callbackRequest.setFlag(customerStatementCallbackRequest.getRESULT().getFLAG());
        callbackRequest.setMessage(customerStatementCallbackRequest.getRESULT().getMESSAGE());

        List<CustomerStatementReportSDO> list = Nullable.stream(customerStatementCallbackRequest.getITEM1()).map(customerStatement->{
            CustomerStatementReportSDO sdo = new CustomerStatementReportSDO();
            sdo.setSalesOrganizationCode(customerStatement.getVKORG());
            sdo.setChannelCode(customerStatement.getVTWEG());
            sdo.setChannelName(customerStatement.getVTEXT());
            sdo.setCustomerCode(customerStatement.getKUNRG());
            sdo.setCustomerName(customerStatement.getKUNAG_TXT());
            sdo.setInvoiceDate(customerStatement.getFKDAT());
            sdo.setDocName(customerStatement.getZDOC_NAME());
            sdo.setActualAmoumt(InfoUtil.trim(customerStatement.getZSH()));
            sdo.setPaymentMethod(customerStatement.getZFKFS());
            sdo.setAccountChangeReason(customerStatement.getZYSZK_BDYY());
            sdo.setInvoiceNo(InfoUtil.deleteZeroPrefix(customerStatement.getVBELN()));
            sdo.setInvoiceLineNo(InfoUtil.deleteZeroPrefix(customerStatement.getPOSNR()));
            sdo.setOrderLineTypeCode(InfoUtil.deleteZeroPrefix(customerStatement.getPOSNR()));
            sdo.setOrderLineTypeDesc(customerStatement.getPSTYV());
            sdo.setAccountantNo(customerStatement.getBELNR());
            sdo.setSalesOrderNo(customerStatement.getAUBEL());
            sdo.setDeliveryOrderNo(customerStatement.getVGBEL());
            sdo.setGoodsNo(InfoUtil.deleteZeroPrefix(customerStatement.getMATNR()));
            sdo.setGoodsName(customerStatement.getARKTX());
            sdo.setStandardPrice(InfoUtil.trim(customerStatement.getNETWR_HS()));
            sdo.setActualPrice(InfoUtil.trim(customerStatement.getNETWR_ZK()));
            sdo.setDiscountRate(InfoUtil.trim(customerStatement.getRATIO()));
            sdo.setQuantity(InfoUtil.trim(customerStatement.getFKIMG()));
            sdo.setUnit(customerStatement.getVRKME());
            sdo.setBasicUnitQuantity(InfoUtil.trim(customerStatement.getFKLMG()));
            sdo.setBasicUnit(customerStatement.getMEINS());
            sdo.setDiscountAmount(InfoUtil.trim(customerStatement.getKZWI6()));
            sdo.setShouldAmoumt(InfoUtil.trim(customerStatement.getKZWI1()));
            sdo.setShouldBalance(InfoUtil.trim(customerStatement.getZYSYE()));
            sdo.setOrderCostDisount(InfoUtil.trim(customerStatement.getZSDFY()));
            sdo.setInvoiceFixedDiscount(InfoUtil.trim(customerStatement.getZK01()));
            sdo.setInvoiceCostDiscount(InfoUtil.trim(customerStatement.getZFPFY()));
            sdo.setCustomerAddressCode(customerStatement.getKUNWE());
            sdo.setCustomerAddressName(customerStatement.getKUNWE_TXT());
            sdo.setCustomerOrderNo(customerStatement.getBSTNK());
            sdo.setProfitCenter(InfoUtil.deleteZeroPrefix(customerStatement.getPRCTR()));
            sdo.setFactoryCode(customerStatement.getHEDI01());
            sdo.setFactoryName(customerStatement.getHEDI02());
            sdo.setRemark1(customerStatement.getHEDI03());
            sdo.setRemark2(customerStatement.getHEDI04());
            sdo.setRemark3(customerStatement.getHEDI05());
            return sdo;
        }).collect(Collectors.toList());

        callbackRequest.setData(list);

        return customerStatementWriteService.callback(callbackRequest);
    }





}
